package cn.vinord.Util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.vinord.Enum.TemplateEnum;
import me.chanjar.weixin.common.exception.WxErrorException;

public class WeixinUtil {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass()); 
	
	private WxMaService service;
	
	public WeixinUtil(WxMaService service) {
		this.service = service;
	}
	
	public  void sendTemplate(String formId,String toUser,String name,String cmutyName){
		try {
			service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder()
			        .templateId(TemplateEnum.PUSH.getCode())
			        .formId(formId)
			        .data(Lists.newArrayList(
			                new WxMaTemplateMessage.Data("keyword1", name),
			                new  WxMaTemplateMessage.Data("keyword2", cmutyName),
			                new WxMaTemplateMessage.Data("keyword3", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()))))
			        .toUser(toUser)
			        .build());
			logger.info("发送给{}开门消息发送成功",toUser);
		} catch (WxErrorException e) {
			logger.error("发送消息失败,%s",e.getMessage());
		}
	}
}
